package com.leon.notepad.content.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luck.picture.lib.entity.LocalMedia;
import com.leon.notepad.R;
import com.leon.notepad.content.bean.NotepadBean;
import com.leon.notepad.content.utils.DateUtils;

import java.util.List;

public class MyNotepadRecyclerAdapter extends RecyclerView.Adapter<MyNotepadRecyclerAdapter.ItemView>{
    private List<NotepadBean> list;
    Context context;
    private OnItemClickListener onItemClickListener;

    // 定义点击回调接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }
    // 定义一个设置点击监听器的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public MyNotepadRecyclerAdapter(List<NotepadBean> list, Context context) {
        this.list=list;
        this.context=context;
    }

    public void updateData(List<NotepadBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public ItemView onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_notepad, viewGroup, false);
        return new ItemView(view);
    }

    @Override
    public void onBindViewHolder(final ItemView itemView, int position) {

        NotepadBean bean = list.get(position);
        itemView.tvContent.setText(bean.getTitle());
        itemView.tvTime.setText(DateUtils.getCurrentTime(bean.getTime()));
        if (bean.getSound() != null && !bean.getSound().equals("")) {
            itemView.ivVideo.setVisibility(View.VISIBLE);
        } else {
            itemView.ivVideo.setVisibility(View.GONE);
        }
        if (bean.getImage() != null && !bean.getImage().equals("")) {
            itemView.ivImage.setVisibility(View.VISIBLE);
            itemView.ivPicture.setVisibility(View.VISIBLE);
            List<LocalMedia> list =  new Gson().fromJson(bean.getImage(), new TypeToken<List<LocalMedia>>(){}.getType());
            Glide.with(context)
                    .load(list.get(0).getPath())
                    .into(itemView.ivPicture);
        } else {
            itemView.ivImage.setVisibility(View.GONE);
            itemView.ivPicture.setVisibility(View.INVISIBLE);
        }
        itemView.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener != null) {
                    int pos = itemView.getLayoutPosition();
                    onItemClickListener.onItemClick(itemView.itemView, pos);
                }
            }
        });
        itemView.bg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onItemClickListener != null) {
                    int pos = itemView.getLayoutPosition();
                    onItemClickListener.onItemLongClick(itemView.itemView, pos);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //viewholder
    public static class ItemView extends  RecyclerView.ViewHolder{


        private TextView tvContent;
        private TextView tvTime;
        private ImageView ivPicture,ivImage,ivVideo;
        private CardView bg;

        public ItemView(View itemView){
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            ivPicture = itemView.findViewById(R.id.iv_picture);
            ivImage = itemView.findViewById(R.id.iv_image);
            ivVideo = itemView.findViewById(R.id.iv_video);
            bg = itemView.findViewById(R.id.bg);
        }


    }

}